<?php
// +----------------------------------------------------------------------
// | 帖子
// +----------------------------------------------------------------------

namespace App\Http\Controllers\App\Forum;

use App\Models\Forum;
use App\Models\ForumCategory;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ForumController extends Controller
{
    /**
     * 列表
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $request = $this->request;
        if ($request->ajax()) {
            $model = Forum::query();
            if ($request->has('category_id')){
                $model = $model->where('category_id',$request->get('category_id'));
            }
            if ($request->has('title')){
                $model = $model->where('title','like','%'.$request->get('title').'%');
            }
            $res = $model->orderBy('created_at','desc')->with(['user','category'])->paginate($request->get('limit',30))->toArray();
            $data = [
                'code' => 0,
                'msg'   => '正在请求中...',
                'count' => $res['total'],
                'data'  => $res['data']
            ];
            return response()->json($data);
        }

        //分类
        $categories = [];
        //$categorys = ForumCategory::with('allChilds')->where('parent_id',0)->orderBy('sort','desc')->get();
        return view('app.forum.list', compact('categories'));
    }

    /**
     * 添加
     */
    public function create()
    {
        $categories = [];
        return view('app.forum.listform',compact('categories'));
    }

    /**
     * 保存
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * 显示
     *
     * @param  \App\Models\Forum  $forum
     * @return View
     */
    public function show(Forum $forum)
    {
        return view('user.profile', ['forum' => $forum]);
    }

    /**
     * 编辑
     *
     * @param  \App\Models\Forum  $forum
     * @return View
     */
    public function edit(Forum $forum)
    {
        $categories = ForumCategory::all();
        return view('app.forum.listform',compact('categories', 'forum'));
    }

    /**
     * 更新
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Forum  $forum
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Forum $forum)
    {
        $this->validate($request->all(), [
            'title' => 'required|max:255',
            'body' => 'required',
        ]);
        dd(1);
        $forum->update($request->all());
    }

    /**
     * 删除
     *
     * @param  mixed $id
     * @return array
     */
    public function destroy($id)
    {
        $id = is_array($id) ? $id : ( is_string($id) ? explode (',', $id) : func_get_args());
        $res = Forum::destroy($id);
        if ( $res > 0 ) {
            $result = ['code' => 0, 'msg' => 'ID:'.$id.' 删除成功！'];
        } else {
            $result = ['code' => 0, 'msg' => 'ID:'.$id.' 删除失败！'];
        }
        return $result;
    }
}
